import { getUnitDelete } from '@/api/Unitregein';
import qs from 'qs';
const numberValue = ref<number>(1);
const treePermission = ref<boolean>(false);
const treeExportData = ref<boolean>(false);
const TreeList = ref<Array<Namespace.unitTree>>();
const treeImortData = ref<boolean>(false);

function xiaweibo() {}
function ElCheckTag(value: number) {
  if (value === 0) {
    formData.formFields.rightdataName = '';
  } else {
    treePermission.value = true;
  }
}

/**
 * 两列表单数据
 * 单元管理
 */
const formData = reactive<Namespace.FormType>({
  loadKey: true,
  formFields: {
    unitCode: '',
    unitName: '',
    dataStatus: '',
    dataId: '',
    unitRight: 0,
    unitPublish: '',
    unitStatus: '',
    unitContent: '',
    remark: '',
    rightdataType: '',
    rightdataName: '',
  },
  formRules: [
    [
      {
        title: '单元编号',
        required: true,
        field: 'unitCode',
        placeholder: '请输入单元编号',
        type: 'text',
      },
      {
        title: '标题',
        required: false,
        field: 'unitName',
        placeholder: '请输入标题',
        type: 'text',
      },
    ],
    [
      {
        dataKey: 'key',
        dataName: 'value',
        title: '显示模式',
        required: true,
        placeholder: '请选择显示模式',
        field: 'unitStatus',
        data: [
          { key: 0, value: '表单' },
          { key: 1, value: '组合' },
        ],
        type: 'radio',
        onChange: xiaweibo, // 方法
      },
      {
        type: 'select',
        dataKey: 'key',
        dataName: 'value',
        default: '', // 默认值 需要异步填写
        title: '数据区域',
        required: false,
        placeholder: '请选择区域',
        field: 'dataId',
        data: [],
      },
    ],
    [
      // {
      //   dataKey: 'key',
      //   dataName: 'value',
      //   title: '需要权限',
      //   required: true,
      //   placeholder: '请选择是否需要权限',
      //   field: 'unitRight',
      //   data: [
      //     { key: 1, value: '使用' },
      //     { key: 0, value: '未使用' },
      //   ],
      //   type: 'radio',
      // },
      {
        dataKey: 'key',
        dataName: 'value',
        title: '是否发布',
        required: false,
        field: 'unitPublish',
        data: [
          { key: 1, value: '发布' },
          { key: 0, value: '未发布' },
        ],
        type: 'radio',
      },
      {
        dataKey: 'key',
        dataName: 'value',
        title: '数据模式',
        required: true,
        field: 'dataStatus',
        placeholder: '请选择数据模式',
        data: [
          { key: 0, value: '请求' },
          { key: 1, value: 'SQL' },
        ],
        type: 'radio',
      },
    ],
    [
      {
        dataKey: 'key',
        dataName: 'value',
        title: '数据权限',
        required: true,
        placeholder: '请选择数据权限',
        field: 'rightdataType',
        data: [
          { key: 1, value: '启用' },
          { key: 0, value: '关闭' },
        ],
        type: 'radio',
        onChange: ElCheckTag,
      },
      {
        title: '数据权限',
        required: false,
        field: 'rightdataName',
        placeholder: '请输入数据权限',
        type: 'text',
        readonly: true,
      },
    ],
    [
      {
        title: '备注',
        required: false,
        field: 'remark',
        colSize: 12,
        type: 'textarea',
      },
    ],
    [
      {
        title: '数据表达式',
        required: false,
        field: 'unitContent',
        colSize: 12,
        type: 'webEditor',
        placeholder: '请输入数据权限',
        language: 'sql',
        switch: true,
      },
    ],
  ],
});
const form = reactive<any>({});

const iconsDataJson = [
  {
    name: 'jstree-region',
    size: 18,
  },
  {
    name: 'jstree-group',
    color: '#606266',
  },
  {
    name: 'jstree-unit',
  },
  {
    name: 'jstree-list',
    size: 18,
  },
  {
    name: 'jstree-query',
  },
  {
    name: 'jstree-wizard',
  },
];

const rightMenuList = [
  {
    iconName: 'jstree-group',
    name: '新增包',
    size: 16,
    function: group,
  },
  {
    iconName: 'jstree-export',
    name: '导出单元',
    size: 25,
    function: exportData,
  },
  {
    iconName: 'jstree-import',
    name: '导入单元',
    size: 25,
    function: importData,
  },
  {
    iconName: 'jstree-unit',
    name: '新增单元',
    size: 25,
    function: unit,
  },
  {
    iconName: 'jstree-list',
    name: '新增列表',
    size: 25,
    function: list,
  },
  {
    iconName: 'jstree-query',
    name: '新增查询',
    size: 25,
    function: query,
  },
  {
    iconName: 'jstree-wizard',
    name: '快速页面',
    size: 25,
    function: wizard,
  },
  {
    iconName: 'jstree-delete',
    name: '删除',
    size: 25,
    function: deleteFunction,
  },
];

/**
 * 数据管理
 */
const iconsDataSourceJson = [
  {
    name: 'jstree-region',
    size: 18,
  },
  {
    name: 'jstree-group',
    color: '#606266',
  },
  {
    name: 'jstree-table',
  },
];

const rightSourceMenuList = [
  {
    iconName: 'jstree-group',
    name: '新增包',
    size: 16,
    function: groupData,
  },
  {
    iconName: 'jstree-table',
    name: '新增表',
    size: 25,
    function: table,
  },
  {
    iconName: 'jstree-delete',
    name: '删除',
    size: 25,
    function: deleteFunction,
  },
];

/** 枚举管理 */

const iconsDataEnumJson = [
  {
    name: 'jstree-region',
    size: 18,
  },
  {
    name: 'jstree-group',
    color: '#606266',
  },
  {
    name: 'jstree-enum',
  },
];

const rightEnumList = [
  {
    iconName: 'jstree-group',
    name: '新增包',
    size: 16,
    function: groupData,
  },
  {
    iconName: 'jstree-enum',
    name: '新增枚举',
    size: 25,
    function: enumRightClick,
  },
  {
    iconName: 'jstree-delete',
    name: '删除',
    size: 25,
    function: deleteFunction,
  },
];

/** sql管理 */

const iconsDataSqlJson = [
  {
    name: 'jstree-region',
    size: 18,
  },
  {
    name: 'jstree-group',
    color: '#606266',
  },
  {
    name: 'jstree-sqlAssign',
  },
  {
    name: 'jstree-sqlAuthen',
  },
];

const rightSqlList = [
  {
    iconName: 'jstree-group',
    name: '新增包',
    size: 16,
    function: groupData,
  },
  {
    iconName: 'jstree-sqlAssign',
    name: '新增赋值sql',
    size: 25,
    function: sqlAssignRightClick,
  },
  {
    iconName: 'jstree-sqlAuthen',
    name: '新增验证sql',
    size: 25,
    function: sqlAuthenRightClick,
  },
  {
    iconName: 'jstree-delete',
    name: '删除',
    size: 25,
    function: deleteFunction,
  },
];

/** 数据权限 */

const iconsDataCataAccessJson = [
  {
    name: 'jstree-region',
    size: 18,
  },
  {
    name: 'jstree-group',
    color: '#606266',
  },
  {
    name: 'jstree-data',
  },
];

const rightCataAccessList = [
  {
    iconName: 'jstree-group',
    name: '新增包',
    size: 16,
    function: groupCataAccess,
  },
  {
    iconName: 'jstree-data',
    name: '新增数据权限',
    size: 18,
    function: cataAccessRightClick,
  },
  {
    iconName: 'jstree-delete',
    name: '删除',
    size: 25,
    function: deleteFunction,
  },
];

/** 右边页面显示参数 */
const rightLowCodeShow = ref<boolean>(false);
const rightDialogShow = ref<boolean>(false);
const type = ref<string>('');
/** ID */
const dataTree = ref<string>();
const unitType = ref<string>('');
export function groupData(data) {
  type.value = 'group';
  unitType.value = '0';
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  form.formFields = {
    unitCode: '',
    unitName: '',
  };
  form.formRules = [
    [
      {
        title: '包名',
        required: true,
        field: 'unitCode',
        placeholder: '请输入包名',
        type: 'text',
      },
      {
        title: '说明',
        required: false,
        field: 'unitName',
        placeholder: '请输入说明',
        type: 'text',
      },
    ],
  ];
  dataTree.value = data.id;
}
export function group(data) {
  type.value = 'group';
  unitType.value = '0';
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  form.loadKey = formData.loadKey;
  form.formFields = {
    unitCode: '',
    unitName: '',
  };
  form.formRules = [
    [
      {
        title: '单元编号',
        required: true,
        field: 'unitCode',
        placeholder: '请输入单元编号',
        type: 'text',
      },
      {
        title: '标题',
        required: false,
        field: 'unitName',
        placeholder: '请输入标题',
        type: 'text',
      },
    ],
  ];

  dataTree.value = data.id;
}
export function importData(data) {
  type.value = 'importData';
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  treeImortData.value = true;
  form.loadKey = formData.loadKey;
  form.formFields = {
    dataId: '',
    cover: '',
    file: [],
  };
  form.formRules = [
    [
      {
        type: 'select',
        dataKey: 'key',
        dataName: 'value',
        default: '',
        title: '数据区域',
        required: true,
        placeholder: '请选择区域',
        field: 'dataId',
        data: [],
      },
      {
        dataKey: 'key',
        dataName: 'value',
        title: '是否覆盖',
        required: true,
        field: 'cover',
        data: [
          { key: 1, value: '是' },
          { key: 0, value: '否' },
        ],
        type: 'radio',
      },
      {
        title: '文件',
        // required: true,
        readonly: false,
        field: 'file',
        limit: 1,
        type: 'upload2',
        // change:'changeFile',
        // data: []
      },
    ],
  ];

  dataTree.value = data.id;
}
export function exportData(data) {
  type.value = 'exportData';
  rightLowCodeShow.value = false;
  rightDialogShow.value = false;
  treeExportData.value = true;
  TreeList.value = [data];
}
export function groupCataAccess(data) {
  type.value = 'group';
  unitType.value = '0';
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  form.loadKey = formData.loadKey;
  form.formFields = {
    unitCode: '',
    unitName: '',
  };
  form.formRules = [
    [
      {
        title: '数据权限编号',
        required: true,
        field: 'unitCode',
        placeholder: '请输入数据权限编号',
        type: 'text',
      },
      {
        title: '数据权限说明',
        required: false,
        field: 'unitName',
        placeholder: '请输入数据权限说明',
        type: 'text',
      },
    ],
  ];

  dataTree.value = data.id;
}
export function unit(data) {
  unitType.value = '1';
  type.value = 'unit';
  rightLowCodeShow.value = false;
  form.loadKey = formData.loadKey;
  form.formFields = formData.formFields;
  form.formRules = formData.formRules;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}
export function list(data) {
  unitType.value = '2';
  type.value = 'list';
  form.loadKey = formData.loadKey;
  form.formFields = formData.formFields;
  form.formRules = formData.formRules;
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}
export function query(data) {
  unitType.value = '3';
  type.value = 'query';
  form.loadKey = formData.loadKey;
  form.formFields = formData.formFields;
  form.formRules = formData.formRules;
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}
export function wizard(data) {
  type.value = 'wizard';
  form.loadKey = formData.loadKey;
  form.formFields = formData.formFields;
  form.formRules = formData.formRules;
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}
export function enumRightClick(data) {
  unitType.value = '5';
  type.value = 'enum';
  form.loadKey = formData.loadKey;
  form.formFields = {
    unitCode: '',
    unitName: '',
    remark: '',
    unitContent: '',
  };
  form.formRules = [
    [
      {
        title: '枚举名',
        required: true,
        field: 'unitCode',
        placeholder: '请输入枚举名',
        type: 'text',
      },
      {
        title: '说明',
        required: false,
        field: 'unitName',
        placeholder: '请输入说明',
        type: 'text',
      },
    ],
    [
      {
        title: '备注',
        required: false,
        field: 'remark',
        colSize: 12,
        type: 'textarea',
      },
    ],
    [
      {
        title: '数据表达式',
        field: 'unitContent',
        type: 'webEditor',
        placeholder: '请输入数据权限',
        language: 'sql',
      },
    ],
  ];
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}

export function sqlAssignRightClick(data) {
  unitType.value = '8';
  type.value = 'assignmentSql';
  form.loadKey = formData.loadKey;
  form.formFields = {
    unitCode: '',
    unitName: '',
    remark: '',
    unitContent: '',
  };
  form.formRules = [
    [
      {
        title: '名称',
        required: true,
        field: 'unitCode',
        placeholder: '请输入名称',
        type: 'text',
      },
      {
        title: '说明',
        required: false,
        field: 'unitName',
        placeholder: '请输入说明',
        type: 'text',
      },
    ],
    [
      {
        title: '备注',
        required: false,
        field: 'remark',
        colSize: 12,
        type: 'textarea',
      },
    ],
    [
      {
        title: '数据表达式',
        field: 'unitContent',
        type: 'webEditor',
        placeholder: '请输入数据权限',
        language: 'sql',
      },
    ],
  ];
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}

export function sqlAuthenRightClick(data) {
  unitType.value = '9';
  type.value = 'verifySql';
  form.loadKey = formData.loadKey;
  form.formFields = {
    unitCode: '',
    unitName: '',
    remark: '',
    unitContent: '',
  };
  form.formRules = [
    [
      {
        title: '名称',
        required: true,
        field: 'unitCode',
        placeholder: '请输入名称',
        type: 'text',
      },
      {
        title: '说明',
        required: false,
        field: 'unitName',
        placeholder: '请输入说明',
        type: 'text',
      },
    ],
    [
      {
        title: '备注',
        required: false,
        field: 'remark',
        colSize: 12,
        type: 'textarea',
      },
    ],
    [
      {
        title: '数据表达式',
        field: 'unitContent',
        type: 'webEditor',
        placeholder: '请输入数据权限',
        language: 'sql',
      },
    ],
  ];
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}

export function table(data) {
  unitType.value = '4';
  type.value = 'table';
  form.loadKey = formData.loadKey;
  form.formFields = {
    unitCode: '',
    unitName: '',
    remark: '',
  };
  form.formRules = [
    [
      {
        title: '表名',
        required: true,
        field: 'unitCode',
        placeholder: '请输入表名',
        type: 'text',
      },
      {
        title: '说明',
        required: false,
        field: 'unitName',
        placeholder: '请输入说明',
        type: 'text',
      },
    ],
    [
      {
        title: '备注',
        required: false,
        field: 'remark',
        colSize: 12,
        type: 'textarea',
      },
    ],
  ];
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}

export function cataAccessRightClick(data) {
  unitType.value = '7';
  type.value = 'cataAccess';
  form.loadKey = formData.loadKey;
  form.formFields = {
    unitCode: '',
    unitName: '',
    remark: '',
  };
  form.formRules = [
    [
      {
        title: '数据权限编号',
        required: true,
        field: 'unitCode',
        placeholder: '请输入数据权限编号',
        type: 'text',
      },
      {
        title: '数据权限说明',
        required: false,
        field: 'unitName',
        placeholder: '请输入数据权限说明',
        type: 'text',
      },
    ],
    [
      {
        title: '备注',
        required: false,
        field: 'remark',
        colSize: 12,
        type: 'textarea',
      },
    ],
  ];
  rightLowCodeShow.value = false;
  rightDialogShow.value = true;
  dataTree.value = data.id;
}

export async function deleteFunction(data, id) {
  if (data.id === id.id) {
    rightLowCodeShow.value = false;
    rightDialogShow.value = false;
  }
  if (data.hasChildren) {
    ElMessage.error('存在子级，请先删除子级');
    return false;
  }
  const JsonData = {
    unitId: data.id,
  };
  getUnitDelete(qs.stringify(JsonData));
  setTimeout(() => {
    numberValue.value++;
  }, 500);
  dataTree.value = data.parentId;
}

export {
  unitType,
  numberValue,
  iconsDataJson,
  rightMenuList,
  dataTree,
  rightLowCodeShow,
  type,
  rightDialogShow,
  form,
  formData,
  iconsDataSourceJson,
  rightSourceMenuList,
  iconsDataEnumJson,
  rightEnumList,
  iconsDataCataAccessJson,
  rightCataAccessList,
  treePermission,
  iconsDataSqlJson,
  rightSqlList,
  treeExportData,
  TreeList,
  treeImortData,
};
